//
//  IntrestView.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/26.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "IntrestView.h"
#import "IntrestCell.h"
#import "ProductModel.h"
#import "PdcDetailVC.h"
//热门推荐URL
#define HOT_URL @"/api/ApiGoods/getRecommendList"
@interface IntrestView ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    
    UIButton *rightNowBtn;
    //瀑布流
    UICollectionView *_collectionView;
    //数据源
    NSMutableArray *_data;
}
@end
@implementation IntrestView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        //图片
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 115*app.autoSizeScaleX, 105*app.autoSizeScaleY)];
        img.center = CGPointMake(WYMWITH/2, 80*app.autoSizeScaleY);
        img.image = [UIImage imageNamed:@"icon_buy_background"];
        [self addSubview:img];
        //lable
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 25)];
        lab.center = CGPointMake(WYMWITH/2, CGRectGetMaxY(img.frame)+40*app.autoSizeScaleY);
        lab.textAlignment = NSTextAlignmentCenter;
        lab.text = @"购物车空空如也";
        [self addSubview:lab];
        //按钮
        rightNowBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        rightNowBtn.backgroundColor = QBB_RED_COLOR;
        rightNowBtn.center = CGPointMake(WYMWITH/2, CGRectGetMaxY(lab.frame)+40*app.autoSizeScaleY);
        rightNowBtn.bounds = CGRectMake(0, 0, WYMWITH - 150*app.autoSizeScaleX, 35*AUTOY);
        [rightNowBtn setTitle:@"马上去夺宝" forState:UIControlStateNormal];
        [rightNowBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightNowBtn addTarget:self action:@selector(rightBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        rightNowBtn.layer.masksToBounds = YES;
        rightNowBtn.layer.cornerRadius = 2;
        [self addSubview:rightNowBtn];
        //感兴趣的商品
        [self loadCollectionView];
        //分割线
        UILabel *inLab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(_collectionView.frame)- 50*app.autoSizeScaleY, WYMWITH, 40*app.autoSizeScaleY)];
        inLab.textColor = [UIColor grayColor];
        inLab.text = @" 您可能感兴趣的商品:";
        [self addSubview:inLab];
        UILabel *fenge2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(inLab.frame), WYMWITH, 0.5)];
        fenge2.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:fenge2];
    }
    return self;
}
//按钮点击事件
- (void)rightBtnOnClick
{
    [[WYMainTabBarVC shareMainTaBarVC]changeSelectedWithIndex:0];
}
/***********
 *加载瀑布流控件
 ************/
- (void)loadCollectionView
{
    //初始化瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 110*app.autoSizeScaleY, WYMWITH, 110*app.autoSizeScaleY) collectionViewLayout:layout];
    //设置代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"IntrestCell" bundle:nil];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];
    [self addSubview:_collectionView];
    
    _data = [[NSMutableArray alloc]initWithArray:[[WYDataBase sharedDataBase]getAllpdcInIntrestCar]];
    if (_data.count > 0) {
        [_collectionView reloadData];
        if (_data.count < 10) {
            [self startRequestForData];
        }
    }else{
        [self startRequestForData];
    }
    
}
//请求网络数据
- (void)startRequestForData
{
    NSDictionary *params = @{@"limit":@"10"};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,HOT_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSArray *pdcs = dic[@"data"];
        for (NSDictionary *pdc in pdcs) {
            WYShopCarModel *model = [[WYShopCarModel alloc]init];
            model.car_id = pdc[@"id"];
            model.car_new_id = pdc[@"sid"];
            model.name = pdc[@"title"];
            model.imgPath = pdc[@"thumb_url"];
            [_data addObject:model];
            //保存
            [[WYDataBase sharedDataBase]insertPdcToIntrestCarWithModel:model];
            [_collectionView reloadData];
        }
        [_collectionView reloadData];
    } failedBlock:^{
        
    } needLog:NO];
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    return CGSizeMake(120*myDelegate.autoSizeScaleX, 110*myDelegate.autoSizeScaleY);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _data.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IntrestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (_data.count>0) {
        //获取当前数据模型
        WYShopCarModel *model = _data[indexPath.row];
        //填充cell
        [cell fillCellWithModel:model];
    }
    //    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return cell;
}
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //保存至兴趣记录
    WYShopCarModel *model = _data[indexPath.row];
//    model.count = 1;
//    [[WYDataBase sharedDataBase]insertPdcToIntrestCarWithModel:model];
//    _data = [[NSMutableArray alloc]initWithArray:[[WYDataBase sharedDataBase]getAllpdcInIntrestCar]];
//    [_collectionView reloadData];
    
    //跳往商品详情
    PdcDetailVC *detailVC = [[PdcDetailVC alloc]init];
    detailVC.pdc_id = model.car_id;
    detailVC.isSidFlag = YES;
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:detailVC animated:YES];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    WYGoodsModel *model = _data[indexPath.row];
    
    return YES;
}
@end
